/*
 * Commande_RotationImage.java
 *
 * Created on 23 janvier 2008, 16:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package Commandes;

import Composite.Photo;
import images.*;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;


public class Commande_Gris implements Commande {
    
    private BufferedImage original;
  
    private BackgroundPanel pan;
    private Photo laPhoto;
    private File dest;
    
    
    public Commande_Gris(BackgroundPanel pan) {
        
        this.pan = pan;
        
        this.laPhoto = pan.getPhoto();
        
    }
    
    
    
    public boolean execute() {
        this.original = (BufferedImage)laPhoto.getImage();
        Date laDate = new Date();
        long leLong = laDate.getTime();
        //   SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
        
        dest = new File("temp/" +  leLong + ".jpg");
        
        try{
            ImageIO.write(original, laPhoto.getType(), dest);     // sauvegarde de l'image avant modification
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
        
        
        
       
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);

ColorConvertOp op = new ColorConvertOp(cs, null);

 

BufferedImage nouveau = op.filter(original, null);
        
        
        
        
        try{
            ImageIO.write(nouveau, laPhoto.getType(), laPhoto.getFichierCourant());     // remplacement de l'original par la nouvelle
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
        pan.setImage(nouveau);
      
        original = null;
        
        return true;
    }
    
    public boolean cancel() {
        //charger original de son emplacement
        //remplacer dans background panel
        //fermer ancienne de la memoire
        
        
        
        try {
            original = ImageIO.read(dest);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        try{
            ImageIO.write(original, laPhoto.getType(), laPhoto.getFichierCourant());     // remplacement de l'original par la nouvelle
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        pan.setImage(original);
        
        original=null;
        
        
        return true;
        
        
        
        
        
        
        
        
    }
    
    public String getNomCommande() {
        return new String(" ( Gris: "+laPhoto.toString() + laPhoto.getExtension() +" ) ");
    }
}


